import {
    Action
} from 'redux';

import * as types from './../types/video';

interface _Action extends Action {
    data:any
}

interface InitState {
    tabs: any,
    likeUser:any,
    comments:any
}

const initState = {
    tabs: {},
    likeUser:{},
    comments:{}
} as InitState;

export default (
    state = initState,
    action: _Action
) => {
    const {
        type,
        data
    } = action;

    switch (type) {
        case types.getAllVideoList : {
            const {
                isPush,
                userId,
                queryList,
                searchType,
            } = data;

            const {
                tabs = {}
            } = state;

            const _key = `${searchType}_${userId}`;

            if (!isPush) {
                tabs[_key] = queryList;
            }
            else {
                const list = tabs[_key] || [];
                tabs[_key] = [...list, ...queryList];
            }

            return {
                ...state,
                tabs: tabs
            }
        }

        case types.getVideoLikeUsers:{
            const {
                isPush,
                videoId,
                queryList
            } = data;

            const {
                likeUser = {}
            } = state;

            if(!isPush) {
                likeUser[videoId]=queryList;
            }
            else {
                const list = likeUser[videoId] || [];
                likeUser[videoId] = [...list, ...queryList];
            }

            return {
                ...state,
                likeUser: likeUser
            }
        }

        case types.getVideoComments:{
            const {
                isPush,
                videoId,
                queryList
            } = data;

            const {
                comments = {}
            } = state;

            if(!isPush) {
                comments[videoId]=queryList;
            }
            else {
                const list = comments[videoId] || [];
                comments[videoId] = [...list, ...queryList];
            }

            return {
                ...state,
                comments: comments
            }
        }
    }
    return state;
}